import { addBuildingAPI, deleteBuildingAPI, editBuildingAPI, fetchBuildingListAPI, findBuildingAPI } from '@/api/park/buildingApi'

const actions = {
  /* 发送请求获取楼宇列表的action */
  async fetchBuildingList(context, pageParams) {
    const { data: buildingListInfo } = await fetchBuildingListAPI(pageParams)
    context.commit('updateBuildingList', buildingListInfo)
  },

  /* 发送请求添加楼宇的action */
  async addBuilding(context, formData) {
    await addBuildingAPI(formData)
    return '添加楼宇成功'
  },

  /* 发送请求查询楼宇的action */
  async findBuilding(context, id) {
    const { data } = await findBuildingAPI(id)
    return data
  },

  /* 发送请求编辑楼宇的action */
  async editBuilding(context, formData) {
    await editBuildingAPI(formData)
    return '编辑楼宇成功'
  },
  /* 发送请求删除楼宇的action */
  async deleteBuilding(context,id){
    await deleteBuildingAPI(id)
    return '删除楼宇成功'
  }
}
const mutations = {
  /* 更新楼宇列表的mutation */
  updateBuildingList(state, buildingListInfo) {
    state.buildingListInfo = buildingListInfo
  }
}
const state = () => {
  return {
    buildingListInfo: {}
  }
}

const getters = {
  // 楼宇列表
  buildingList: state => state.buildingListInfo.rows,
  // 楼宇列表的数据总条数
  total: state => state.buildingListInfo.total
}

export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
